package com.mf.ergate.controller.auth;

import com.mf.ergate.api.auth.RoleResourceRelationRemoteService;
import com.mf.ergate.api.auth.model.dto.RoleResourceRelationCreateDto;
import com.mf.ergate.api.auth.model.dto.RoleResourceRelationDeleteDto;
import com.mf.ergate.api.auth.model.dto.RoleResourceRelationQueryDto;
import com.mf.ergate.api.auth.model.dto.RoleResourceRelationUpdateDto;
import com.mf.ergate.api.auth.model.vo.RoleResourceRelationVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.auth.RoleResourceRelationEntity;
import com.mf.ergate.service.auth.RoleResourceRelationService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色与资源关联Controller
 * User: zhaoming
 * DateTime: 2019-02-08
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class RoleResourceRelationController implements RoleResourceRelationRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleResourceRelationService roleResourceRelationService;

    /**
     * 新增
     * @param roleResourceRelationCreateDto 角色与资源关联新增Dto
     * @return 角色与资源关联
     */
    public Result<RoleResourceRelationVo> insert(@RequestBody RoleResourceRelationCreateDto roleResourceRelationCreateDto) {
        RoleResourceRelationEntity roleResourceRelationEntity = MfBeanUtils.copyObject(roleResourceRelationCreateDto, RoleResourceRelationEntity.class);
        roleResourceRelationEntity = roleResourceRelationService.insert(roleResourceRelationEntity);
        RoleResourceRelationVo roleResourceRelationVo = MfBeanUtils.copyObject(roleResourceRelationEntity, RoleResourceRelationVo.class);
        return new Result<>(roleResourceRelationVo);
    }

    /**
     * 根据唯一编号更新
     * @param roleResourceRelationUpdateDto 角色与资源关联更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody RoleResourceRelationUpdateDto roleResourceRelationUpdateDto) {
        RoleResourceRelationEntity roleResourceRelationEntity = MfBeanUtils.copyObject(roleResourceRelationUpdateDto, RoleResourceRelationEntity.class);
        Boolean flag = roleResourceRelationService.updateByUniqueNo(roleResourceRelationEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param roleResourceRelationDeleteDto 角色与资源关联删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody RoleResourceRelationDeleteDto roleResourceRelationDeleteDto){
        RoleResourceRelationEntity roleResourceRelationEntity = MfBeanUtils.copyObject(roleResourceRelationDeleteDto, RoleResourceRelationEntity.class);
        Boolean flag = roleResourceRelationService.deleteByUniqueNo(roleResourceRelationEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 角色与资源关联
     */
    public Result<RoleResourceRelationVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        RoleResourceRelationEntity roleResourceRelationEntity = roleResourceRelationService.selectByUniqueNo(uniqueNo);
        RoleResourceRelationVo roleResourceRelationVo = MfBeanUtils.copyObject(roleResourceRelationEntity, RoleResourceRelationVo.class);
        return new Result<>(roleResourceRelationVo);
    }

    /**
     * 根据条件查询
     * @param roleResourceRelationQueryDto 角色与资源关联查询Dto
     * @return 角色与资源关联列表
     */
    public Result<List<RoleResourceRelationVo>> selectByCondition(@RequestBody RoleResourceRelationQueryDto roleResourceRelationQueryDto) {
        List<RoleResourceRelationEntity> roleResourceRelationEntities = roleResourceRelationService.selectByCondition(roleResourceRelationQueryDto);
        List<RoleResourceRelationVo> roleResourceRelationVos = MfBeanUtils.copyList(roleResourceRelationEntities, RoleResourceRelationVo.class);
        return new Result<>(roleResourceRelationVos);
    }

    /**
     * 根据条件查询数量
     * @param roleResourceRelationQueryDto 角色与资源关联查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody RoleResourceRelationQueryDto roleResourceRelationQueryDto) {
        return new Result<>(roleResourceRelationService.selectCount(roleResourceRelationQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 角色与资源关联分页列表
     */
    public Result<Page<RoleResourceRelationVo>> selectPage(@RequestBody RemotePage<RoleResourceRelationQueryDto> remotePage){
        Page<RoleResourceRelationEntity> roleResourceRelationEntityPage = roleResourceRelationService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<RoleResourceRelationVo> page = MfBeanUtils.copyPage(roleResourceRelationEntityPage, RoleResourceRelationVo.class);
        return new Result<>(page);
    }

    /**
     * 保存角色与菜单关联关系
     * @param roleResourceRelationCreateDtos 角色与资源关联新增Dto列表
     */
    public Result<Boolean> save(@RequestBody List<RoleResourceRelationCreateDto> roleResourceRelationCreateDtos){
        List<RoleResourceRelationEntity> roleResourceRelationEntities = MfBeanUtils.copyList(roleResourceRelationCreateDtos, RoleResourceRelationEntity.class);
        Boolean flag = roleResourceRelationService.save(roleResourceRelationEntities);
        return new Result<>(flag);
    }

}